package com.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import com.beans.Admin;
import com.beans.User;
import org.springframework.web.servlet.ModelAndView;

@Slf4j
@Controller
@RequestMapping("/user")
public class UserController {

    @Resource
    UserService service;

    @RequestMapping("/login")
    public String login() {
        return "login";
    }

    @PostMapping("/gotologin")
    public String login(String userName, String password, String role, ModelMap m, HttpSession session, HttpServletRequest request) {
        if ("admin".equals(role)) {
            Admin admin = service.loging(userName, password);
            session.setAttribute("userInfo", admin);
            if (admin != null) {
                request.getSession().setAttribute("session_user", admin);
                m.put("msg", userName);
                return "main1";
            } else {
                m.put("msg", "登陆失败，请重新输入");
                return "login";
            }
        } else if ("user".equals(role)) {
            User user = service.login(userName, password);
            session.setAttribute("userInfo", user);
            if (user != null) {
                m.put("msg", userName);
                session.setAttribute("shelf", userName);
                request.getSession().setAttribute("session_user", user);
                return "main";
            } else {
                m.put("msg", "登陆失败，请重新输入");
                return "login";
            }
        } else {
            return "login";
        }
    }

    @GetMapping("/logout")
    public ModelAndView userlogout(HttpServletRequest request){
        User user = (User) request.getSession().getAttribute("userInfo");
        //session失效
        request.getSession().invalidate();
        if (user != null) {
            log.info(user.getUserName() + "安全退出");
        }
        return new ModelAndView("redirect:/user/login");
    }

    @RequestMapping("/register")
    public String tongzhi() {
        return "register";
    }

    @RequestMapping("/addregister")
    public String register(User user, ModelMap m) {
        service.register(user);
        m.put("msg", "注册成功!");
        return "register";
    }

    @RequestMapping("/get")
    public String get(String userName, ModelMap model) {
        System.out.println("get==>" + userName);
        if (userName != null && !userName.equals("")) {
            List<User> list = service.get(userName);
            model.addAttribute("list", list);
            return "user/alluser";
        }
        model.addAttribute("error", "未查询到响应的数据，请您检查输入的数据名称是否正确~");
        System.err.println("error");
        return "user/alluser";
    }


    @PostMapping("/gotoupdateuser")
    public String updateu(User user, ModelMap m) {
        m.put("user", user);
        service.updateUser(user);
        m.put("msg", "修改成功");
        return "forward:/user/getalluser";
    }




    @RequestMapping("/adduser")
    public String adduser(User user, ModelMap m) {
        service.register(user);
        m.put("msg", "添加成功！");
        return "forward:/user/gotoadduser";
    }

    @RequestMapping("/index")
    public String index() {
        return "index";
    }

    @RequestMapping("/center")
    public String center(ModelMap m,HttpSession session) {
        User user = (User) session.getAttribute("userInfo");
        m.put("user", user);
        return "center/index";

    }

    @GetMapping("/updatecenter")
    public String updatcenter(int id, ModelMap m) {
        User user = service.getUserById(id);
        m.put("user", user);
        return "center/updatecenter";
    }


    @RequestMapping("/shouye")
    public String logis() {
        return "user/shouye";
    }

    /***************** 【以下为修改部分内容】 *********************/

    @RequestMapping("/getalluser")
    public String getalluser() {
        return "user/alluser";
    }

    @RequestMapping("/queryByPage")
    @ResponseBody
    public Map<String, Object> queryByPage(User user, Integer page, Integer limit) {
        return service.queryByPage(user, page, limit);
    }

    @PostMapping("/gotoupdatecenter")
    @ResponseBody
    public Integer updateuw(@RequestBody User user) {
        return  service.updateUser(user);
    }


    @RequestMapping("/deleteuser")
    @ResponseBody
    public Integer deleteu(int id) {
        return  service.deleteUser(id);
    }
}

